<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Decliner</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="classics.css" rel="stylesheet" type="text/css">
</head>

<?php
function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    $start  = $length * -1; //negative
    return (substr($haystack, $start) === $needle);
}

$nouns = array();

function addNoun($sNom, $stem, $type) {
	global $nouns;
	$nouns[$sNom]['stem'] = $stem;
	$nouns[$sNom]['type'] = $type;
}

$endings = array();

function addNEndings($type, $number, $nom, $gen, $dat, $acc, $voc, $abl) {
	global $endings;
	$endings[$type][$number] = array('nominative' => $nom, 'genitive' => $gen, 'dative' => $dat, 'accusative' => $acc, 'vocative' => $voc, 'ablative' => $abl);
}

addNEndings('1', 'singular', 'a', 'ae', 'ae', 'am', 'NULL', 'ā');
addNEndings('1', 'plural', 'ae', 'ārum', 'īs', 'ās', 'NULL', 'īs');

addNEndings('2us', 'singular', '', 'ī', 'ō', 'um', 'e', 'ō');
addNEndings('2us', 'plural', 'ī', 'ōrum', 'īs', 'ōs', 'NULL', 'īs');
addNEndings('2um', 'singular', '', 'ī', 'ō', 'um', 'NULL', 'ō');
addNEndings('2um', 'plural', 'a', 'ōrum', 'īs', 'a', 'NULL', 'īs');
addNEndings('2r', 'singular', '', 'ī', 'ō', 'um', 'NULL', 'ō');
addNEndings('2r', 'plural', 'ī', 'ōrum', 'īs', 'ōs', 'NULL', 'īs');

addNEndings('3', 'singular', '', 'is', 'ī', 'em', 'NULL', 'e');
addNEndings('3', 'plural', 'ēs', 'um', 'ibus', 'ēs', 'NULL', 'ibus');
addNEndings('3l.r.n.s_n', 'singular', '', 'is', 'ī', '', 'NULL', 'e');
addNEndings('3l.r.n.s_n', 'plural', 'a', 'um', 'ibus', 'a', 'NULL', 'ibus');
addNEndings('3is', 'singular', '', 'is', 'ī', 'em', 'NULL', 'ī|e');
addNEndings('3is', 'plural', 'ēs', 'ium', 'ibus', 'īs|ēs', 'NULL', 'ibus');
addNEndings('3e.al.ar', 'singular', '', 'is', 'ī', '', 'NULL', 'ī');
addNEndings('3e.al.ar', 'plural', 'ia', 'ium', 'ibus', 'ia', 'NULL', 'ibus');
addNEndings('3mixed', 'singular', '', 'is', 'ī', 'em', 'NULL', 'e');
addNEndings('3mixed', 'plural', 'ēs', 'ium', 'ibus', 'ēs|īs', 'NULL', 'ibus');


addNEndings('4us', 'singular', '', 'ūs', 'uī|ū', 'um', 'NULL', 'ū');
addNEndings('4us', 'plural', 'ūs', 'uum', 'ibus', 'ūs', 'NULL', 'ibus');

addNEndings('4ū', 'singular', '', 'ūs', 'ū', 'ū', 'NULL', 'ū');
addNEndings('4ū', 'plural', 'ua', 'uum', 'ibus', 'ua', 'NULL', 'ibus');

addNEndings('5', 'singular', '', 'ēī', 'ēī', 'em', 'NULL', 'ē');
addNEndings('5', 'plural', 'ēs', 'ērum', 'ēbus', 'ēs', 'NULL', 'ēbus');

function decline($noun, $stem, $type) {
	global $endings;
	$result = array();
	foreach($endings[$type] as $number => $aNumber) {
		foreach($aNumber as $case => $ending) {
			if ($ending == '') {
				$result[$number][$case] = "'$noun'";
			} elseif ($ending == 'NULL') {
				$result[$number][$case] = 'NULL';
			} else {
				$tmpa = explode('|', $ending);
				$tmpb = array();
				foreach($tmpa as $ed) {
					$tmpb[] = $stem.$ed;
				}
				$result[$number][$case] = "'".implode('|', $tmpb)."'";
			}
		}
	}
	return $result;
}

function validateForm() {
	return ($_SERVER['REQUEST_METHOD'] == 'POST') && $_POST['noun'] 
		   && $_POST['gender'] && $_POST['meaning'] && $_POST['stem'];
}

$defaults['declension'] = $endings[0];
$defaults['noun'] = '';
$defaults['gender']['m.'] = '';
$defaults['gender']['f.'] = '';
$defaults['gender']['n.'] = '';
$defaults['meaning'] = '';
$defaults['stem'] = '';

if (validateForm()) {
	$defaults['declension'] = $_POST['declension'];
	$defaults['noun'] = $_POST['noun'];
	foreach($_POST['gender'] as $gender) {
		$defaults['gender'][$gender] = ' checked="checked"';
	}
	$defaults['meaning'] = $_POST['meaning'];
	$defaults['stem'] = $_POST['stem'];
}
?>

<script type="text/javascript">
function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
</script>

<body>
<div id="container">
<div id="center">

<form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post">
<table align="center">
<caption><b>Latin Noun Decliner</b></caption>

<tr class="separate-above"><th>Type: </th>
<td>
<select name="declension">
<?php

foreach($endings as $dtype => $aDeclesion) {
	echo "<option value=\"$dtype\"";
	if ($defaults['declension'] == $dtype) {
		echo " SELECTED";
	}
	echo ">$dtype</option>\n";
}
?>
</select>
</td>
</tr>
<tr><th>Noun: </th><td><input name="noun" type="text" value="<?php echo $defaults['noun']; ?>"></td></tr>
<tr><th>Gender: </th>
	<td>
<?php
	foreach($defaults['gender'] as $gender => $checked) {
		echo "<input type=\"checkbox\" name=\"gender[]\" value=\"$gender\" $checked/>$gender\n";
	}
?>
	</td>
</tr>
<tr><th>Meaning: </th><td><input name="meaning" type="text" value="<?php echo $defaults['meaning']; ?>"></td></tr>
<tr><th>Stem: </th><td><input name="stem" type="text" value="<?php echo $defaults['stem']; ?>"></td></tr>
<tr class="separate-above"><td></td><td><input type="submit" value="Submit"/></td></tr>
</table>
</form>

</div>
</div>

<?php
if (validateForm()) {
	$decls = decline($_POST['noun'], $_POST['stem'], $_POST['declension']);
	$th = "";
	$sql = "BEGIN;\n";
	foreach($decls as $number => $aNumber) {
		$isth = ! $th;
		
		$tr[$number] = "<td>".$_POST['noun']."</td>";
		$tr[$number] .= "<td>".implode(" & ", $_POST['gender'])."</td>";
		$tr[$number] .= "<td>$number</td>";
		$tr[$number] .= "<td>".$_POST['meaning']."</td>";
		
		$sql .= "INSERT INTO nouns VALUES('".$_POST['noun']."', '".implode(" & ", $_POST['gender'])."', ";
		$sql .= "'$number', '".$_POST['meaning']."', ";
		$sql .= implode(", ", $aNumber);
		foreach($aNumber as $case => $word) {
			if ($isth) {
				$th .= "<th>$case</th>";
			}
			$tr[$number] .= "<td>$word</td>";
		}
		$sql .= ");\n";
	}
	$sql .= "COMMIT;\n";
	$th = "<th>noun</th><th>gender</th><th>number</th><th>meaning</th>$th";
?>
<hr />
<div id="container">
<div id="center">
<table align="center" border=1>
<?php
echo "<tr>$th</tr>\n";
echo "<tr>".$tr['singular']."</tr>\n";
echo "<tr>".$tr['plural']."</tr>\n";
?>
</table>
<hr />
<textarea align="center" style='font-size:8pt;' cols="120" rows="10" wrap="off" id="sqls" onClick="SelectAll('sqls');">
<?php echo $sql; ?>
</textarea>
</div>
</div>

<?php
}
?>
</body>
</html>
